var util = require('../../utils/util.js');
var api = require('../../config/api.js');

Page({
  data: {
    fenleiList: [],
    goodsList: [],
    id: 0,
    scrollLeft: 0,
    scrollTop: 0,
    scrollHeight: 0,
    page: 1,
    limit: 10
  },
  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;

    if (options.id) {
      that.setData({
        id: parseInt(options.id)
      });
    }
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    });
    this.twofenleilist();
    this.getFenleiDetail();
  },
  twofenleilist: function() {
    let that = this;
    util.request(api.GoodsTwoFenleiList, {
        id: that.data.id
      })
      .then(function(res) {
        if (res.result == 1) {
          that.setData({
            fenleiList: res.data
          });
          //nav位置
          let currentIndex = 0;
          let fenleiListCount = that.data.fenleiList.length;
          for (let i = 0; i < fenleiListCount; i++) {
            currentIndex += 1;
            if (that.data.fenleiList[i].id == that.data.id) {
              break;
            }
          }
          if (currentIndex > fenleiListCount / 2 && fenleiListCount > 5) {
            that.setData({
              scrollLeft: currentIndex * 60
            });
          }

        } else {
          //显示错误信息
        }

      });
  },
  getFenleiDetail: function() {
    let that = this;
    util.request(api.GoodsFenleiDetail, {
      id: that.data.id,
      page: that.data.page,
      limit: that.data.limit
    }).then(function(res) {
      if (res.result == 1) {
        if (res.data.length > 0) {
          that.setData({
            goodsList: that.data.goodsList.concat(res.data),
            page: that.data.page + 1
          });
        } else {
          wx.showToast({
            title: '已经被您看完了!',
            icon: 'none',
            duration: 2000
          });
          return false;
        }
        //nav位置
        let currentIndex = 0;
        let fenleiListCount = that.data.fenleiList.length;
        for (let i = 0; i < fenleiListCount; i++) {
          currentIndex += 1;
          if (that.data.fenleiList[i].id == that.data.id) {
            break;
          }
        }
        if (currentIndex > fenleiListCount / 2 && fenleiListCount > 5) {
          that.setData({
            scrollLeft: currentIndex * 60
          });
        }

      } else {
        //显示错误信息
      }

    });
  },
  onReady: function() {
    // 页面渲染完成
  },
  onShow: function() {
    // 页面显示
    console.log(1);
  },
  onHide: function() {
    // 页面隐藏
  },

  onUnload: function() {
    // 页面关闭
  },
  switchCate: function(event) {
    if (this.data.id == event.currentTarget.dataset.id) {
      return false;
    }
    var that = this;
    var clientX = event.detail.x;
    var currentTarget = event.currentTarget;
    if (clientX < 60) {
      that.setData({
        scrollLeft: currentTarget.offsetLeft - 60
      });
    } else if (clientX > 330) {
      that.setData({
        scrollLeft: currentTarget.offsetLeft
      });
    }
    this.setData({
      id: event.currentTarget.dataset.id,
      goodsList: [],
      page: 1
    });

    this.getFenleiDetail();
  },
  onReachBottom: function() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.getFenleiDetail();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  }
})